The following code snippet allows us to export the Excel table header, we can pass it as an array to generate our dynamic table header, and at the same time call it as exporter.ExportHeaderAsByteArray
and export our result
of type byte[]
to the specified filepath(path ).
public async Task ExportHeaderAsByteArrayWithItems_Test()
{
IExcelExporter exporter = new ExcelExporter();
var arr = new[] { "Name1", "Name2", "Name3", "Name4", "Name5", "Name6" };
var sheetName = "Test";
var result = await exporter.ExportHeaderAsByteArray(arr, sheetName);
result.ToExcelExportFileInfo(filePath);
}
In this export exportDatas
is a List, but in the following code snippet we have converted it to a DataTable and exported it, maxRowNumberOnASheet
is the number of rows each sheet holds.
public async Task LargeDataDynamicExport_Test()
{
IExcelExporter exporter = new ExcelExporter();
var dt = new DataTable();
//Create columns with column names and type names
dt.Columns.Add("Text", Type.GetType("System.String"));
dt.Columns.Add("Name", Type.GetType("System.String"));
dt.Columns.Add("Number", Type.GetType("System.Decimal"));
dt = EntityToDataTable(dt, exportDatas);
var result = await exporter.Export(filePath, dt, maxRowNumberOnASheet: 100000);
}
ExpandoObject
is a dynamic object that is part of the Dynamic Language (DLR). With ExpandoObject
we can add and remove members of its instances at runtime, and of course set and get these values. In IE we support ExpandoObject
objects, so here we can use ExpandoObject
to implement our dynamic export behavior.
public async Task ExportAsByteArraySupportDynamicType_Test()
{
IExporter exporter = new ExcelExporter();
var source = GenFu.GenFu.ListOf<ExportTestDataWithAttrs>();
string fields = "text,number,name";
var shapedData = source.ShapeData(fields) as ICollection<ExpandoObject>;
var result = await exporter.ExportAsByteArray<ExpandoObject>(shapedData);
File.WriteAllBytes(filePath, result);
}
In addition, for ExpandoObject export process for table headers and other information, you can use filters to achieve, please refer to the tutorial article on the use of filters for details.